package com.wzq.animal.fore.controller;

import com.wzq.animal.common.Result;
import com.wzq.animal.fore.pojo.Swiper;
import com.wzq.animal.fore.service.SwiperService;
import com.wzq.animal.utils.MyOSSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/swiper")
public class SwiperController {
    @Autowired
    private SwiperService swiperService;

    /**
     * 获取轮播图数据
     * @return
     */
   @GetMapping("/getSwipers")
   public Result getSwipers(){
       List<Swiper> swipers = swiperService.list();
       return Result.success(swipers);
   }

    /**
     * 上传轮播图到阿里 OSS
     * @param file
     * @return
     * @throws IOException
     */
   @PostMapping("/uploadSwiper")
    public Result uploadSwiper(@RequestParam MultipartFile file) throws IOException {
       String location = "swiper";
       String url = MyOSSUtils.uploadFile(file, location);
       return Result.success(url);
   }

    /**
     * 修改轮播文件信息
     * @param swiper
     * @return
     */
   @PostMapping("/updateSwiper")
    public Result updateSwiper(@RequestBody Swiper swiper){
       swiperService.updateById(swiper);
       return Result.success();
   }
}
